# coding:utf-8
import time
import tkinter as tk

import ttkbootstrap as tkBoot

from util.mysql_util import add_students
from util.ui_util import show_center


class AddStudentBox(tkBoot.Toplevel):
    def __init__(self, parent, class_id):
        super().__init__(parent)
        self.parent = parent
        self.class_id = class_id
        self.number = tk.StringVar()
        self.name = tk.StringVar()
        self.age = tk.IntVar()

    def initFrame(self):
        width = int(self.winfo_screenwidth()/6)
        height = int(self.winfo_screenheight()/4)
        self.title("新建班级")
        show_center(self, width, height)
        self.resizable(False, False)

        idFrame = tkBoot.Frame(self)
        tkBoot.Label(idFrame, text="学号").pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Entry(idFrame, textvariable=self.number).pack(side=tk.RIGHT)
        idFrame.pack(pady=(20, 0))

        nameFrame = tkBoot.Frame(self)
        tkBoot.Label(nameFrame, text="姓名").pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Entry(nameFrame, textvariable=self.name).pack(side=tk.RIGHT)
        nameFrame.pack(pady=(20, 0))

        ageFrame = tkBoot.Frame(self)
        tkBoot.Label(ageFrame, text="年龄").pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Entry(ageFrame, textvariable=self.age).pack(side=tk.RIGHT)
        ageFrame.pack(pady=(20, 0))

        btnFrame = tkBoot.Frame(self)
        tkBoot.Button(btnFrame, text="确认", command=self.confirm).pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Button(btnFrame, text="取消", command=self.cancel).pack(side=tk.RIGHT)
        btnFrame.pack(side=tk.BOTTOM, pady=(0, 20))

    def confirm(self):
        rows = [(self.class_id, self.number.get(), self.name.get(), self.age.get())]
        self.parent.refresh_table_data()
        add_students(rows[0])
        time.sleep(0.1)
        self.destroy()

    def cancel(self):
        self.destroy()
